package org.acb.sgp.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.acb.sgp.beans.Programa;
import org.acb.sgp.dao.ProgramaDao;
import org.acb.sgp.factory.ConnectionFactory;

public class ProgramaDaoImpl implements ProgramaDao {

    private Connection con;
    private CallableStatement cs;
    private ResultSet rs;

    private Connection getConnection() throws SQLException {
        return ConnectionFactory.getInstance().getConnection();
    }

    @Override
    public void registrarPrograma(Programa programa) {
        try {
            String sql = "{Call RegistrarPrograma(?,?,?,?,?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setString(1, programa.getNomPrograma());
            cs.setString(2, programa.getNomGenero());
            cs.setString(3, programa.getStrSinpsis());
            cs.setInt(4, programa.getEstado().getCodEstado());
            if (programa.getCodPrograma() == null) {
                cs.setInt(5, 0);
                cs.registerOutParameter(5, Types.INTEGER);
                cs.execute();
                programa.setCodPrograma((Integer) cs.getObject(5));
            } else {
                cs.setInt(5, programa.getCodPrograma());
                cs.execute();
            }
        } catch (SQLException e) {
            System.out.println("ERROR EN ProgramaDaoImpl>registrarPrograma: " + e.toString());
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
    }

    @Override
    public void eliminarPrograma(Programa programa) {
    }

    @Override
    public Programa buscarProgramaPorCodigo(Integer codPrograma) {
        Programa programa = new Programa();
        try {
            String sql = "{Call BuscarProgramaPorCodigo(?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, codPrograma);
            rs = cs.executeQuery();
            if (rs.next()) {
                programa = llenar(rs);
            }
        } catch (SQLException e) {
            System.out.println("ERROR EN ProgramaDaoImpl>buscarProgramaPorCodigo: " + e.toString());
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return programa;
    }

    @Override
    public List<Programa> listaProgramas() {
        List<Programa> programas = new ArrayList<Programa>();
        try {
            String sql = "{Call ListarProgramas()}";
            con = getConnection();
            cs = con.prepareCall(sql);
            rs = cs.executeQuery();
            while (rs.next()) {
                programas.add(llenar(rs));
            }
        } catch (SQLException e) {
            System.out.println("ERROR EN ProgramaDaoImpl>listaProgramas: " + e.toString());
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return programas;
    }

    private Programa llenar(ResultSet rs) throws SQLException {
        Programa programa = new Programa();
        programa.setCodPrograma(rs.getInt("codPrograma"));
        programa.setNomPrograma(rs.getString("nomPrograma"));
        programa.setNomGenero(rs.getString("nomGenero"));
        programa.setStrSinpsis(rs.getString("strSinopsis"));
        programa.getEstado().setCodEstado(rs.getInt("codEstado"));
        programa.getEstado().setNomEstado(rs.getString("nomEstado"));
        programa.getEstado().getTipoEstado().setCodTipo(rs.getInt("codTipo"));
        programa.getEstado().getTipoEstado().setNomTipo(rs.getString("nomTipo"));
        return programa;
    }

}
